<?php

class Myzen_Utilities_UserDetails
{    
	//the end points are public accessible, to avoid having someone log in by just accessing the end point with user details, we create a signature	
	
	//convert a number into column name to generate the excel sheet 1 => A, 2 => B....
	static public function generateSignature( $userDetails) {
		
		$_forwardSecret  = Zend_Registry::get('dt_config')->auth->signature;
		if(empty($_forwardSecret)) Throw new Myzen_Exception('auth.signature mandatory on config file and missing');
		
		$time = $userDetails['authDetails']['signature']['time'];
		return sha1($_forwardSecret.$time.$userDetails['authDetails']['user']['email']);
    }

	//return true if the signature is valid, false otherwise
	static public function isSignatureValid($userDetails) {
		
		$_forwardSecret  = Zend_Registry::get('dt_config')->auth->signature;
		if(empty($_forwardSecret)) Throw new Myzen_Exception('auth.signature mandatory on config file and missing');
		
		$signatureValue = @$userDetails['signature']['value'];
		if(empty($signatureValue)) return false;
		$time = 	@$userDetails['signature']['time'];
		//limited to 5 minutes
		//if($time < time() - 5*60) return false;
		
		$email = @$userDetails['user']['email'];
		
		$calculatedSignature = 	sha1($_forwardSecret.$time.$email);
		
		if($signatureValue === $calculatedSignature) return true;
		return false;	
		
	} //end isSignatureValid	
	
	
	
} //end class